class Solution {
public:
    string longestPalindrome(string s)
    {
        if (s.size() == 0) return "";
        if (s.size() < 2) return s;
        int n = s.size(), max = 0, begin = 0;
        for (int i = 0; i < n; i++)
        {
            int left = i, right = i;
            while (left >= 0 && right < n && s[left] == s[right])
            {
                left--;
                right++;
            }
            if (right - left - 1 > max)
            {
                begin = left + 1;
                max = right - left - 1;
            }
            left = i;
            right = i + 1;
            while (left >= 0 && right < n && s[left] == s[right])
            {
                left--;
                right++;
            }
            if (right - left - 1 > max)
            {
                begin = left + 1;
                max = right - left - 1;
            }
        }
        return s.substr(begin, max);
    }
};